package com.jcf.springboot.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcf.springboot.common.R;
import com.jcf.springboot.entity.sys.SysRole;
import com.jcf.springboot.service.SysRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jjj
 * @since 2025-03-18
 */
@RestController
@RequestMapping("/role")
public class SysRoleController {

  @Autowired
  private SysRoleService roleService;

  @GetMapping("getList")
  @ApiOperation("我是角色列表接口")
  public R getList(String query, Integer pagenum, Integer pagesize) {
    Page<SysRole> page = new Page<>(pagenum, pagesize);
    QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
    if (!StringUtils.isEmpty(query))
    {
      queryWrapper.like("name", query);
    }
    roleService.page(page, queryWrapper);
    List<SysRole> list = page.getRecords();

    long total = page.getTotal();
    Map<Object, Object> map = new HashMap<>();
    map.put("value", list);
    map.put("total", total);
    if (!list.isEmpty())
    {
      return R.ok().message("获取角色列表成功").data(map);

    }
    else
    {
      return R.error().message("获取角色列表失败");

    }
  }

  @GetMapping("getAll")
  public R getAll(){
    List<SysRole> list = roleService.list(new QueryWrapper<>());
    if(!ObjectUtils.isEmpty(list)){
      return R.ok().message("获取角色列表成功").data(list);
    }
    else{
      return R.error().message("获取角色列表失败");
    }
  }

  @GetMapping("getRoleById")
  public R getRoleById(@RequestParam String id){
    SysRole role = roleService.getById(id);
    return R.ok().message("获取角色信息成功").data(role);

  }

  @GetMapping("/getRoleByUser")
  public R getRoleByUser(String userId){
    List<String> resultList = roleService.getRoleByUser(userId);
    return R.ok().message("获取角色信息成功").data(resultList);

  }


  @PostMapping("addRole")
  public R addRole(@RequestBody SysRole role) {
    boolean b = roleService.save(role);
    if (b)
    {
      return R.ok().message("添加角色成功");
    }
    else
    {
      return R.error().message("添加角色失败");
    }
  }

  @PostMapping("editRole")
  public R editRole(@RequestBody SysRole role) {
    boolean flag = roleService.updateById(role);
    if (flag)
    {
      return R.ok().message("编辑成功");
    }
    else
    {
      return R.error().message("编辑失败");
    }

  }

  @DeleteMapping("deleteRole/{id}")
  public R deleteManager(@PathVariable String id){
    boolean b = roleService.removeById(id);
    if(b){
      return R.ok().message("删除成功");
    } else {
      return R.error().message("删除失败");
    }
  }



  @DeleteMapping("batchDelete")
  public R batchDelete(@RequestParam("idList") List<String> idList){
    boolean b = roleService.removeByIds(idList);
    if(b){
      return R.ok().message("删除成功");
    } else {
      return R.error().message("删除失败");
    }
  }
}

